//
//  RadioDetailsViewController.m
//  aMoment
//
//  Created by dllo on 15/10/2.
//  Copyright © 2015年 LuckyHan. All rights reserved.
//

#import "RadioDetailsViewController.h"
#import "AllHeaderFiles.h"

@interface RadioDetailsViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)RadioDetailsHeaderView *headerView;
@property(nonatomic, retain)MBProgressHUD *progressView;
@property(nonatomic, retain)NSMutableArray *listArray;
@end

@implementation RadioDetailsViewController

- (void)dealloc
{
    [_tableView release];
    [_headerView release];
    [_progressView release];
    [_listArray release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.dataSourceArray = [NSMutableArray array];
    self.listArray = [NSMutableArray array];
    [self getData];
    
    //刷新加载
    _tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.start = 0;
        [self getData];
    }];
//    [_tableView.header beginRefreshing];
    _tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self.start += 10;
        [self getData];
    }];
}

#pragma mark 获取数据
- (void)getData
{
    if (!self.start) {//下拉刷新
        self.urlString = URLSTRING_RADIO_DETAILS;
        self.bodyString = BODYSTRING_RADIO_DEATILS;
    }
    else {//上拉加载
        self.urlString = @"http://api2.pianke.me/ting/radio_detail_list";
        self.bodyString = BODYSTRING_RADIO_DEATILS;
    }
    [NetworkRequest getDataWithURLString:self.urlString BodyString:self.bodyString DataBlock:^(id data) {
        //移除加载中提示视图
        [_progressView removeFromSuperview];
        
        //设置总收听量图标
        UIImageView *leftView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, IMAGE_16, IMAGE_16)];
        leftView.image = [UIImage imageNamed:@"iconfont-listen"];
        _headerView.listenNumField.leftView = leftView;
        [leftView release];
        
        if (!self.start) {//下拉刷新
            [self.dataSourceArray removeAllObjects];
            [self.listArray removeAllObjects];
        }
        [self.listArray addObjectsFromArray:[[(NSDictionary *)data objectForKey:@"data"] objectForKey:@"list"]];

        NSArray *listArray = [NSArray arrayWithArray:[[(NSDictionary *)data objectForKey:@"data"] objectForKey:@"list"]];
        for (NSDictionary *dict in listArray) {
            RadioDetails *radioDetails = [[[RadioDetails alloc] init] autorelease];
            [radioDetails setValuesForKeysWithDictionary:dict];
            [self.dataSourceArray addObject:radioDetails];
        }
        if (!self.start) {
            _headerView.radioInfoDic = [[(NSDictionary *)data objectForKey:@"data"] objectForKey:@"radioInfo"];
            self.title = [_headerView.radioInfoDic objectForKey:@"title"];
        }
        [_tableView reloadData];
        [_tableView.header endRefreshing];
        [_tableView.footer endRefreshing];
    }];
}

- (void)loadView
{
    [super loadView];
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.rowHeight = 50 * FIT_WIDTH + 20 * FIT_HEIGHT;
    self.headerView = [[RadioDetailsHeaderView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 265 * FIT_HEIGHT + IMAGE_32)];
    _tableView.tableHeaderView = _headerView;
    [_headerView release];
    _tableView.tableFooterView = [[UIView alloc] init];
    [self.view addSubview:_tableView];
    [_tableView release];
    
    //创建加载中提示视图
    self.progressView = [LoadingProgress createViewWithSuperviewFrame:self.view.frame];
    [self.view addSubview:_progressView];
    [_progressView release];
}

#pragma mark cell个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSourceArray.count;
}

#pragma mark cell样式
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    RadioDetailsTableCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[[RadioDetailsTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.radioDetails = self.dataSourceArray[indexPath.row];
    return cell;
}

#pragma mark 点击cell方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    Music *music = [[Music alloc] init];
    RadioDetails *radioDerails = self.dataSourceArray[indexPath.row];
    music.musicURL = radioDerails.musicUrl;
    music.coverImageURL = radioDerails.coverimg;
    music.songName = radioDerails.title;
    music.singer = [[radioDerails.playInfo objectForKey:@"userinfo"] objectForKey:@"uname"];
    music.tingId = radioDerails.tingid;
    
    //跳转到详情页
    RadioDetailsInfoViewController *detailsVC = [[RadioDetailsInfoViewController alloc] init];
    [self.navigationController pushViewController:detailsVC animated:YES];
    detailsVC.index = indexPath.row;
    detailsVC.tingId = [self.dataSourceArray[indexPath.row] tingid];
    detailsVC.listArray = _listArray;
    [detailsVC release];
    
    //通知中心
    [[MusicSingleton shareInstance] playMusicWith:music.musicURL];
    //改变音乐播放条
    [[MusicSingleton shareInstance] setMusicModel:music];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
